package com.lesent.grpc.server;

import com.lesent.grpc.demo.GreeterGrpc;
import com.lesent.grpc.demo.HelloReply;
import com.lesent.grpc.demo.HelloRequest;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;

import java.util.concurrent.TimeUnit;

/**
 * @author shengtao
 * @Description:
 * @date 2018/08/14 13:34
 */
public class ClientServer {

    private final ManagedChannel channel;
    private final GreeterGrpc.GreeterBlockingStub greeterBlockingStub;

    public ClientServer(String host,int port) {
        channel = ManagedChannelBuilder.forAddress(host,port)
                .enableRetry()
                .usePlaintext()
                .build();
        greeterBlockingStub = GreeterGrpc.newBlockingStub(channel);

    }

    public void shutdown() throws InterruptedException{
        channel.awaitTermination(5, TimeUnit.SECONDS);
    }

    public static void mian(String[] args){
        ClientServer clientServer = new ClientServer("127.0.0.1",50051);
        for (int i=0;i<100;i++){
            HelloRequest request = HelloRequest.newBuilder().setName("你好i"+i).build();
            HelloReply reply = clientServer.greeterBlockingStub.sayHello(request);
            System.out.println(reply.getMessage());
        }
    }

}
